package com.huike.web.controller.clues;

import com.huike.clues.service.ITbClueService;
import com.huike.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.result.UserInfoAjaxResult;
import com.huike.clues.domain.vo.AssignmentVo;
import com.huike.clues.domain.vo.FalseClueVo;
import com.huike.clues.domain.vo.TbClueExcelVo;
import com.huike.clues.service.ITbClueService;
import com.huike.common.constant.HttpStatus;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * @Description 线索管理Controller
 * @Author YEmeeyo
 * @Date 2023-07-12 15:39
 */

@RestController
@RequestMapping("/clues/clue")
@Slf4j
@Api(tags = "线索管理相关接口")
public class TbClueController extends BaseController {
    @Resource
    private ITbClueService tbClueService;

    /**
     * 伪线索
     *
     * @param falseClueVo
     * @param id
     * @return
     */
    @PutMapping("/false/{id}")
    @ApiOperation(("伪线索"))
    public AjaxResult falseClue(@RequestBody FalseClueVo falseClueVo, @PathVariable Long id) {
        log.info("转换为伪线索");
        tbClueService.falseClue(falseClueVo, id);
        return AjaxResult.success();
    }

    /**
     * 批量捞取线索
     *
     * @param assignmentVo
     * @return
     */
    @ApiOperation(("批量捞取线索"))
    @PutMapping("/gain")
    public AjaxResult gain(@RequestBody AssignmentVo assignmentVo) {
        log.info("批量捞取线索");

        if (assignmentVo.getIds().length > 3) {
            return AjaxResult.error("最多选择3条线索!");
        }
        tbClueService.gain(assignmentVo);
        return AjaxResult.success();
    }

    /**
     * TODO
     *
     * @param file
     * @return
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) {
        try (
                FileInputStream in = new FileInputStream((File) file);
        ) {

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询线索管理列表
     *
     * @param clue
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(("查询线索管理列表"))
    public TableDataInfo list(TbClue clue) {
        log.info("查询线索管理列表");
        startPage();
        List<TbClue> clueList = tbClueService.selectClueList(clue);

        TableDataInfo tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(clueList);
        tableDataInfo.setTotal(new PageInfo(clueList).getTotal());

        return tableDataInfo;
    }

    /**
     * 查询线索池
     *
     * @param clue
     * @return
     */
    @GetMapping("/pool")
    public TableDataInfo pool(TbClue clue) {
        startPage();
        List<TbClue> clueList = tbClueService.selectCluePool(clue);

        TableDataInfo tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(clueList);
        tableDataInfo.setTotal(new PageInfo(clueList).getTotal());

        return tableDataInfo;
    }

    @Resource
    ITbClueService iTbClueService;
    @PostMapping()
    @ApiOperation("新增线索管理")
    public UserInfoAjaxResult insert(@RequestBody TbClue tbClue){
        log.info("新增线索管理，操作人:{}", SecurityUtils.getUsername());
        iTbClueService.insert(tbClue);
        return UserInfoAjaxResult.success();
    }
    @PutMapping()
    @ApiOperation("修改线索管理")
    public UserInfoAjaxResult update(@RequestBody TbClue tbClue){
        log.info("修改线索管理，操作人:{}，修改对象id：{}", SecurityUtils.getUsername(),tbClue.getId());
        iTbClueService.update(tbClue);
        return UserInfoAjaxResult.success();
    }



/**
 * @Description TODO
 * @Author hzh
 * @Date 2023-07-12 22:04
 */


    /**
     * 根据id获取线索基础信息 回显
     * @param id
     * @return TbClue
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取线索基础信息")
    public AjaxResult findClueById(@PathVariable Long id) {
        return AjaxResult.success(tbClueService.findClueById(id));
    }
}
